package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.job.ums.entity.UmsAccount;
import org.jeecg.modules.job.ums.entity.UmsWithdraw;
import org.jeecg.modules.job.ums.mapper.UmsAccountMapper;
import org.jeecg.modules.job.ums.mapper.UmsWithdrawMapper;
import org.jeecg.modules.job.ums.service.IUmsWithdrawService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Description: 用户提现
 * @Author: jeecg-boot
 * @Date:   2023-03-30
 * @Version: V1.0
 */
@Service
public class UmsWithdrawServiceImpl extends ServiceImpl<UmsWithdrawMapper, UmsWithdraw> implements IUmsWithdrawService {

    @Resource
    private UmsAccountMapper accountMapper;

    @Override
    public boolean add(UmsWithdraw param) {
        //判断提现金额是否合理
        UmsAccount account=accountMapper.selectOne(new LambdaQueryWrapper<UmsAccount>().eq(UmsAccount::getMemberId,param.getMemberId()));
        if (account!=null && account.getBalanceWithdraw().compareTo(param.getMoney())>=0){
            //可以提现
            param.setLastBalance(account.getBalanceWithdraw());
            param.setBalance(account.getBalanceWithdraw().subtract(param.getMoney()));
            param.setWithdrawStatus(0);//待审核
            this.save(param);
            //更改账户余额
            UmsAccount accountParam=new UmsAccount();
            accountParam.setId(account.getId());
            accountParam.setBalanceFrozen(account.getBalanceFrozen().add(param.getMoney()));//冻结余额
            accountParam.setBalanceWithdraw(account.getBalanceWithdraw().subtract(param.getMoney()));
            accountMapper.updateById(accountParam);
//            //添加提现记录
//            MemberAccountRecordsParam recordsParam=new MemberAccountRecordsParam();
//            recordsParam.setType(2);
//            recordsParam.setSign(0);
//            recordsParam.setMoney(param.getMoney());
//            recordsParam.setMemberId(param.getMemberId());
//            recordsParam.setDataId(param.getId());
//            recordsParam.setContent("提现申请");
//            accountRecordsService.add(recordsParam);
        }else{
            throw new RuntimeException("当前可提现余额不足");
        }
        return true;
    }
}
